<?php
/**
 * User: @lixu
 * Date: 2019-05-28
 * Time: 11:09
 */

use Biz\service\wiki\Search as sSearch;
use Biz\service\wiki\search\Schedule;
use Doraemon\tools\ActionLog;
use \Doraemon\service\MultiContent\SearchRecommend as sMSearchRecommend;
use Doraemon\pockets\constInterface\ABTest\MarkManage;
use Doraemon\service\ABTest\GetTestStrategy as sGetTestStrategy;
use Doraemon\tools\BlockType;
use Doraemon\tools\StatSign;

class SearchAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;

    public function main()
    {
        $sortTypeMap = [
            '默认',
            '最新',
        ];

        $keyword = $this->getRequest()->getQuery('keyword', '');
        if (!$keyword) {
            throw new \Exception('请输入关键字');
        }
        
        $page = $this->getRequest()->getQuery('page', 0);
        $page = $page < 1 ? 1 : abs($page);

        $from = $this->getRequest()->getQuery('from', '');

        $sortType = $this->getRequest()->getQuery('sort_type', 0);
        $searchRequestId = $this->getRequest()->getQuery('search_request_id', '');

        // 接收回传数据
        $passBackData = (string)$this->getRequest()->get('pb', "");

        if (empty($passBackData)) {
            $passBackData = json_encode(array(
                "history_count" => 0
            ));
        }

        // https://jira.haohaozhu.me/browse/HHZML-574 特定品牌词汇走新的暂定逻辑
        $brandId = \Doraemon\service\MultiContent\MultiContent::getBrandIdByKeyword($keyword);
//        $list = Schedule::search($page, $sortType, $keyword, $brandId, $searchRequestId, $passBackData, $from);
        $list = Schedule::searchInGo($page, $sortType, $keyword, $brandId, $searchRequestId, $passBackData, $from);

        // 中央空调 删除 某个wiki id 为了安抚 土城， 后续整个召回 都会重构 临时修改下
        if ($keyword == "中央空调怎么选" && $page == 1) {
            foreach ($list["list"] as $index => $item) {
                if ($item["id"] == 234679) {
                    unset($list["list"][$index]);
                }
            }
        }

        $list['list'] = StatSign::versionCompatible($list['list'], StatSign::WIKI_UP_GRADE_VERSION_522, BlockType::TYPE_WIKI, 'wiki');
        $list["cache_key"] = md5("111");

        $passBackDataArr = json_decode($passBackData, true);

        $passBackDataArr["history_count"] = $passBackDataArr["history_count"] + count($list["list"]);
        $list["pb"] = json_encode($passBackDataArr);

        $info = [
            'keyword' => $keyword,
            'page' => $page,
            'sort_type' => $sortTypeMap[$sortType],
            'form' => $from,
            'tab' => $this->getRequest()->getQuery('tab', ''),
            'search_request_id' => $searchRequestId,
        ];

        ActionLog::log($this->uid, $info, ActionLog::BIZ_WIKI_SEARCH);

        $this->setSuccess($list);
    }
}